package com.aliyun.smartlight.service.impl;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.aliyun.smartlight.constants.ErrorCode;
import com.aliyun.smartlight.exception.AddDataException;
import com.aliyun.smartlight.exception.BaseException;
import com.aliyun.smartlight.exception.DeleteErrorException;
import com.aliyun.smartlight.exception.UpdateException;
import com.aliyun.smartlight.mapper.CategoryMapper;
import com.aliyun.smartlight.mapper.DeviceMapper;
import com.aliyun.smartlight.pojo.Category;
import com.aliyun.smartlight.pojo.Device;
import com.aliyun.smartlight.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by jackiechan on 2020/12/9 22:57
 *
 * @Author jackiechan
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void addCategory(Category category) {

        if (category == null || category.isNull()) {
            throw new AddDataException("数据为空", ErrorCode.NULL_DATA);
        }

        try {
            categoryMapper.addCategory(category);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddDataException(e.getMessage(), ErrorCode.DATA_NOT_ALLOWED);

        }
    }

    @Override
    public void updateCategory(Category category) {
        if (category == null || category.getId() == null || (StringUtils.isEmpty(category.getCommandsjson()) && StringUtils.isEmpty(category.getCategoryname()))) {
            throw new UpdateException("分类信息不完整", ErrorCode.DATA_NOT_ALLOWED);
        }
        try {
            if ("0".equalsIgnoreCase(category.getStatus())) {
                List<Device> deviceList = deviceMapper.getAllDevices(null, -100, -100, -100, category.getId().intValue());
                if (deviceList.size() > 0) {//其实此处只需要查询数量即可,不用查询具体的内容
                    throw new UpdateException("不能删除当前分类,当前分类中有设备", ErrorCode.FAIL);
                }
            }
            categoryMapper.updateCategory(category);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpdateException(e.getMessage(), ErrorCode.DATA_NOT_ALLOWED);

        }
    }

    @Override
    public Category getCategoryById(Long id) {
        if (id == null) {
            throw new BaseException("id 不能为空", ErrorCode.DATA_NOT_ALLOWED);
        }
        try {
            Category category = categoryMapper.getCategoryById(id);
            if (category == null) {
                throw new BaseException("无数据", ErrorCode.NULL_DATA);
            }
            return category;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage(), ErrorCode.NULL_DATA);
        }
    }

    @Override
    public PageInfo<Category> getAllCategories(int pageNum, int pageSize, String cateoryname, int status) {
        PageHelper.startPage(pageNum, pageSize);
        List<Category> allCategories = categoryMapper.getAllCategories(cateoryname, status);
        PageInfo<Category> pageInfo = new PageInfo<>(allCategories);
        return pageInfo;
    }

    @Override
    public void deleteCategories(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new DeleteErrorException("没有传递 id", ErrorCode.NULL_DATA);
        }
        try {
            for (String id : ids) {
                List<Device> deviceList = deviceMapper.getAllDevices(null, -100, -100, -100, Integer.parseInt(id));
                if (deviceList.size() > 0) {//其实此处只需要查询数量即可,不用查询具体的内容
                    throw new UpdateException("不能删除当前分类,当前分类中有设备", ErrorCode.FAIL);
                }
            }
            categoryMapper.deleteCategories(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
